package awk.designModel.listener.listener;

import awk.designModel.listener.event.ApplicationContextPreparedEvent;
import awk.designModel.listener.event.ContextPreparedEvent;
import awk.designModel.listener.event.ServletContextPreparedEvent;
import awk.designModel.listener.eventsource.ApplicationContext;
import awk.designModel.listener.eventsource.Context;
import java.util.Map;

/**
 * @description:
 * @author: wanghaifeng
 * @date: 2020/12/28
 */
public class MyContextListener extends ContextListener {

    public MyContextListener(ContextPreparedEvent event) {
        super(event);
    }


    public void starting() {
        //堆事件源做业务处理
        ContextPreparedEvent event = getEvent();
        Context context = event.getSource();
        Map<String, Object> attributes = context.getAttributes();
        if (context instanceof ApplicationContext) {
            attributes.put("ApplicationContextPreparedEvent","single");
        }
        //对事件做业务处理
        if (this.event instanceof ServletContextPreparedEvent) {
            attributes.put("context", "servletContext");
            System.out.println("机器人 servlet 工作提示：请看管好的你机器人，防止它偷懒！");
        } else if (this.event instanceof ApplicationContextPreparedEvent) {
            attributes.put("context", "applicationContext");
            System.out.println("机器人 application 工作提示：请看管好的你机器人，防止它偷懒！");
        }
    }

    public void onEvent() {
        //对事件做业务处理
        if (this.event instanceof ServletContextPreparedEvent) {
            System.out.println("机器人 servlet 跳舞提示：机器人跳舞动作优美，请不要走神哦！");
        } else if (this.event instanceof ApplicationContextPreparedEvent) {
            System.out.println("机器人 application 跳舞提示：机器人跳舞动作优美，请不要走神哦！");
        }
    }
}
